package com.ruoyi.xag.ysty.controller;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.xag.service.DeviceService;
import com.ruoyi.xag.ysty.domain.BusinessAndDeviceId;
import com.ruoyi.xag.ysty.domain.YSTYDeviceInfo;
import com.ruoyi.xag.ysty.service.YSTYDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Api("云上田野设备相关")
@Slf4j
@RestController
@RequestMapping("/YSTY/device")
public class YSTYDeviceController extends BaseController {

    @Autowired
    private YSTYDeviceService ystyDeviceService;
    @Autowired
    private DeviceService deviceService;


    @GetMapping("/getDeviceList")
    public AjaxResult getDeviceList(){
        List<YSTYDeviceInfo> lst = ystyDeviceService.selectAllYSTYDevice();
        return AjaxResult.success(lst);
    }

    /**
     * 从云上田野拉取设备信息并添加到数据库
     * @param
     * @return
     */

    @GetMapping("/getDeviceFromYSTY")
    public AjaxResult getDeviceFromYSTY(String business) throws ParseException {
        int i = ystyDeviceService.insertDeviceInfoFromYSTY(business);
        if(i>0){
            return AjaxResult.success("插入完成");
        }else {
            return AjaxResult.error("和云上田野相同，无需更新");
        }
    }

    @GetMapping("/getDeviceElement")
    public AjaxResult getDeviceElement(BusinessAndDeviceId businessAndDeviceId){
        String result = ystyDeviceService.getDeviceElement(businessAndDeviceId);
        JSONObject jsonObject = JSON.parseObject(result);
        return AjaxResult.success("查询成功，设备属性列表如下：" + jsonObject);
    }

    @ApiOperation("后端测试接口，请忽略！")
    @GetMapping("/dataInsert")
    public AjaxResult dataInsert(BusinessAndDeviceId businessAndDeviceId) {
        String soilId = businessAndDeviceId.getDeviceId();
        System.out.println(soilId);
        try {
            int soil = 0;
            YSTYDeviceInfo soilDevice = ystyDeviceService.selectYSTYDevice(soilId);
            soil = ystyDeviceService.insertSoilDataPointLast(businessAndDeviceId);
            // 根据返回值判断影响行数，决定是否获取数据成功
            return (soil > 0) ? AjaxResult.success("获取成功" + soilDevice) : AjaxResult.error("获取最近一条数据失败!");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("获取失败！");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    @ApiOperation("后端测试云上田野的气象站的接口，请忽略！")
    @GetMapping("/weatherInsert")
    public AjaxResult weatherInsert(BusinessAndDeviceId businessAndDeviceId){
        System.out.println(businessAndDeviceId);
        try {
            int weather = 0;
            String weatherId = businessAndDeviceId.getDeviceId();
            YSTYDeviceInfo weatherDevice = ystyDeviceService.selectYSTYDevice(weatherId);
            weather = ystyDeviceService.insertWeatherDataPointLast(businessAndDeviceId);
            // 根据返回值判断影响行数，决定是否获取数据成功
            return (weather > 0) ? AjaxResult.success("获取成功" + weatherDevice) : AjaxResult.error("获取最近一条数据失败!");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
